/*
 * MyApplication.java
 *
 * Created on June 24, 2013, 2:51 PM
 */
package com.example.vaadin;

import com.vaadin.Application;
import com.vaadin.ui.*;
import com.vaadin.data.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 *
 * @author CrownMarek
 * @version
 */
public class MyApplication extends Application {

    //main window    
    private Window main = new Window("Historical weather info");
    //menu buttons
    private Button HomeButton = new Button("Home");
    private Button SearchButton = new Button("Search");
    private Button AboutButton = new Button("About");
    //labels
    private Label logo = new Label("<img src='" + "http://localhost:8084/weather-in-cz-gui/logo.jpg" + "' />", Label.CONTENT_XHTML);
    private Label HomeLable = new Label();
    private Label AboutLabel = new Label();
    //additional buttons
    private Button sbM = new Button("Search by month");
    private Button sbR = new Button("Search by region");
    private Button sb3 = new Button("Search");
    private Button sb4 = new Button("Search");
    private Button backTosb1FromM = new Button("Back");
    private Button backTosb1FromR = new Button("Back");
    //grid layout
    private GridLayout grid = new GridLayout(2, 2);
    //additional layouts
    private VerticalLayout SL1 = new VerticalLayout();
    private VerticalLayout SL2Region = new VerticalLayout();
    private VerticalLayout SL2Month = new VerticalLayout();
    private VerticalLayout Results = new VerticalLayout();
    //additional components and parameters
    private String year, month, region;
    private Select select = new Select("Year");
    private Select selectMonth = new Select("Month");
    private Select selectRegion = new Select("Region");

    //Initializes the the whole GUI
    @Override
    public void init() {

        initWindow();
        initButtons();
        setMainWindow(main);
        setTheme("my-chameleon-black");
    }

    //Initializes the main window
    public void initWindow() {
        // menu layout

        Label title = new Label("<font size=" + "7" + "><B>Historical weather in CZ</B></font>", Label.CONTENT_XHTML);
        VerticalLayout up = new VerticalLayout();
        up.addComponent(title);
        up.setComponentAlignment(title, Alignment.MIDDLE_CENTER);


        HorizontalLayout menu = new HorizontalLayout();
        menu.setHeight("65px");

        up.addComponent(menu);

        HomeButton.setHeight("60px");
        HomeButton.setWidth("250px");

        SearchButton.setHeight("60px");
        SearchButton.setWidth("250px");

        AboutButton.setHeight("60px");
        AboutButton.setWidth("250px");

        HomeButton.addStyleName("wide tall big bold");
        SearchButton.addStyleName("wide tall big");
        AboutButton.addStyleName("wide tall big");


        menu.addComponent(HomeButton);
        menu.setComponentAlignment(HomeButton, Alignment.BOTTOM_CENTER);

        menu.addComponent(SearchButton);
        menu.setComponentAlignment(SearchButton, Alignment.BOTTOM_CENTER);

        menu.addComponent(AboutButton);
        menu.setComponentAlignment(AboutButton, Alignment.BOTTOM_CENTER);


        menu.setSpacing(true);

        //SL1 layout

        SL1.setHeight("210px");
        SL1.setWidth("750px");

        Label q1 = new Label();
        q1.setValue("<div align=\"center\">Choose a year which interests you.</div>");
        q1.setContentMode(Label.CONTENT_XHTML);
        q1.addStyleName("h1");

        sbM.addStyleName("big default");
        sbR.addStyleName("big default");
        HorizontalLayout HSL1 = new HorizontalLayout();

        for (int i = 1961; i <= 2013; i++) {
            select.addItem(Integer.toString(i));
        }
        select.setValue("1961");

        HSL1.setWidth("600px");
        HSL1.addComponent(sbM);
        HSL1.setComponentAlignment(sbM, Alignment.MIDDLE_LEFT);
        HSL1.setExpandRatio(sbM, 1);
        HSL1.addComponent(sbR);
        HSL1.setComponentAlignment(sbR, Alignment.MIDDLE_RIGHT);
        HSL1.setExpandRatio(sbR, 1);
        HSL1.setSpacing(true);


        SL1.addComponent(q1);
        SL1.setComponentAlignment(q1, Alignment.MIDDLE_CENTER);
        SL1.addComponent(select);
        SL1.setComponentAlignment(select, Alignment.MIDDLE_CENTER);
        SL1.addComponent(HSL1);
        SL1.setComponentAlignment(HSL1, Alignment.MIDDLE_CENTER);

        //SL2Month layout
        SL2Month.setHeight("210px");
        SL2Month.setWidth("750px");
        selectMonth.addItem("January");
        selectMonth.addItem("February");
        selectMonth.addItem("March");
        selectMonth.addItem("April");
        selectMonth.addItem("May");
        selectMonth.addItem("June");
        selectMonth.addItem("July");
        selectMonth.addItem("August");
        selectMonth.addItem("September");
        selectMonth.addItem("October");
        selectMonth.addItem("November");
        selectMonth.addItem("December");

        Label q3 = new Label("<div align=\"center\">Choose the month which interests you.</div>", Label.CONTENT_XHTML);
        q3.addStyleName("h1");

        HorizontalLayout HSL2M = new HorizontalLayout();

        selectMonth.setValue("January");
        SL2Month.addComponent(q3);
        SL2Month.setComponentAlignment(q3, Alignment.MIDDLE_CENTER);
        SL2Month.addComponent(selectMonth);
        SL2Month.setComponentAlignment(selectMonth, Alignment.MIDDLE_CENTER);


        backTosb1FromM.addStyleName("big default");
        sb3.addStyleName("big default");
        HSL2M.setWidth("600px");
        HSL2M.addComponent(backTosb1FromM);
        HSL2M.setComponentAlignment(backTosb1FromM, Alignment.MIDDLE_LEFT);
        HSL2M.setExpandRatio(backTosb1FromM, 1);
        HSL2M.addComponent(sb3);
        HSL2M.setComponentAlignment(sb3, Alignment.MIDDLE_RIGHT);
        HSL2M.setExpandRatio(sb3, 1);
        HSL2M.setSpacing(true);

        SL2Month.addComponent(HSL2M);
        SL2Month.setComponentAlignment(HSL2M, Alignment.MIDDLE_CENTER);

        //SL2Region layout
        SL2Region.setHeight("210px");
        SL2Region.setWidth("750px");
        selectRegion.addItem("ceska republika");
        selectRegion.addItem("praha a stredocesky");
        selectRegion.addItem("jihocesky");
        selectRegion.addItem("plzensky");
        selectRegion.addItem("karlovarsky");
        selectRegion.addItem("ustecky");
        selectRegion.addItem("liberecky");
        selectRegion.addItem("kralovehradecky");
        selectRegion.addItem("pardubicky");
        selectRegion.addItem("vysocina");
        selectRegion.addItem("jihomoravsky");
        selectRegion.addItem("olomoucky");
        selectRegion.addItem("zlinsky");
        selectRegion.addItem("moravskoslezsky");

        Label q4 = new Label("<div align=\"center\">Choose the region which interests you.</div>", Label.CONTENT_XHTML);
        q4.addStyleName("h1");

        HorizontalLayout HSL2R = new HorizontalLayout();

        selectRegion.setValue("ceska republika");
        SL2Region.addComponent(q4);
        SL2Region.setComponentAlignment(q4, Alignment.MIDDLE_CENTER);
        SL2Region.addComponent(selectRegion);
        SL2Region.setComponentAlignment(selectRegion, Alignment.MIDDLE_CENTER);

        backTosb1FromR.addStyleName("big default");
        sb4.addStyleName("big default");
        HSL2R.setWidth("600px");
        HSL2R.addComponent(backTosb1FromR);
        HSL2R.setComponentAlignment(backTosb1FromR, Alignment.MIDDLE_LEFT);
        HSL2R.setExpandRatio(backTosb1FromR, 1);
        HSL2R.addComponent(sb4);
        HSL2R.setComponentAlignment(sb4, Alignment.MIDDLE_RIGHT);
        HSL2R.setExpandRatio(sb4, 1);
        HSL2R.setSpacing(true);

        SL2Region.addComponent(HSL2R);
        SL2Region.setComponentAlignment(HSL2R, Alignment.MIDDLE_CENTER);

        //Result layout

        //Resource file = new ClassResource("googlemap.html",  Results.getApplication());
        //Embedded emb = new Embedded("TEST", file);
        //emb.setType(Embedded.TYPE_BROWSER);
        //emb.setWidth("100%");
        //emb.setHeight("100%");

        //Results.addComponent(emb);
        //Label FIN = new Label("<iframe src=\"googlemap.html\" width = \"740\" height = \"440\" frameborder = \"0\"></iframe>", Label.CONTENT_XHTML);
        //Results.addComponent(FIN);
        //Results.setComponentAlignment(FIN, Alignment.MIDDLE_CENTER);

        //grid layout

        grid.setSpacing(true);

        logo.setHeight("110px");
        grid.addComponent(logo, 0, 0);

        grid.addComponent(up, 1, 0);

        grid.removeComponent(1, 1);
        VerticalLayout vl = new VerticalLayout();
        vl.setWidth("750px");
        vl.addComponent(new Label("<h1 align=\"center\">Welcome to our project homepage!\n<h1>"
                + "<h3>We made this application for you to visualize historical weather data in Czech Republic since 1961.\n We are students and this project was created for a subject PB138 Modern markup languages at FI MUNI Brno.\n</h3>"
                + "<h3>We are proud to present the result of our work.</h3>", Label.CONTENT_XHTML));
        grid.addComponent(vl, 1, 1);

        main.addComponent(grid);
    }

    //Initializes all buttons 
    public void initButtons() {
        HomeButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                grid.removeComponent(1, 1);
                VerticalLayout vl = new VerticalLayout();
                vl.setWidth("750px");
                vl.addComponent(new Label("<h1 align=\"center\">Welcome to our project homepage!\n<h1>"
                        + "<h3>We made this application for you to visualize historical weather data in Czech Republic since 1961.\n We are students and this project was created for a subject PB138 Modern markup languages at FI MUNI Brno.\n</h3>"
                        + "<h3>We are proud to present the result of our work.</h3>", Label.CONTENT_XHTML));
                grid.addComponent(vl, 1, 1);
            }
        });

        SearchButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                grid.removeComponent(1, 1);
                grid.addComponent(SL1, 1, 1);
            }
        });

        AboutButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                grid.removeComponent(1, 1);
                VerticalLayout vl = new VerticalLayout();
                vl.setWidth("750px");
                vl.addComponent(new Label("<h2 align=\"center\">This project serves as beautiful Czech historical weather data mining system for \n masses who love graphs.\n</h2>"
                        + "\n"
                        + "<h1 align=\"center\">Developers:\n<h1>"
                        + "\n"
                        + "<h2 align=\"center\">Marek Odráška - Java frontend (Vaadin)\n</h2>"
                        + "<h2 align=\"center\">Michal Šťava - Java backend\n</h2>"
                        + "<h2 align=\"center\">Martin Bednařík - GoogleMaps\n</h2>"
                        + "<h2 align=\"center\">Matúš Honěk - graphs</h2>", Label.CONTENT_XHTML));
                grid.addComponent(vl, 1, 1);
            }
        });

        sbM.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                year = select.getValue().toString();
                grid.removeComponent(1, 1);
                grid.addComponent(SL2Month, 1, 1);
            }
        });

        sbR.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                year = select.getValue().toString();
                grid.removeComponent(1, 1);
                grid.addComponent(SL2Region, 1, 1);
            }
        });

        sb3.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                month = selectMonth.getValue().toString();
                region = "";
                try {
                    Results.removeAllComponents();
                    String pathToXml = GraphsParser.xmlPath + year + ".xml";
                    GraphsParser.getPathToHTML(pathToXml, GraphsParser.xslPath, "/home/norexan/NetBeansProjects/weather-in-cz-gui/web/output.html", new Integer(monthToInteger()));
                    GraphsParser.getPathToGraph(pathToXml, GraphsParser.xqPath, "/home/norexan/NetBeansProjects/weather-in-cz-gui/web/outputXQ.html", year, "", monthToInteger().toString(), pathToXml);
                    Thread.sleep(1500);
                    Results.addComponent(new Label("<iframe src=\"http://localhost:8084/weather-in-cz-gui/output.html\" width = \"740\" height = \"440\" frameborder = \"0\"></iframe>", Label.CONTENT_XHTML));
                    Results.addComponent(new Label("<iframe src=\"http://localhost:8084/weather-in-cz-gui/outputXQ.html\" width = \"620\" height = \"420\" frameborder = \"0\"></iframe>", Label.CONTENT_XHTML));
                } catch (Exception ex) {
                    System.out.println(ex);
                }
                grid.removeComponent(1, 1);
                grid.addComponent(Results, 1, 1);
            }
        });

        sb4.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                region = selectRegion.getValue().toString();
                month = "";
                try {
                    Results.removeAllComponents();
                    String pathToXml = GraphsParser.xmlPath + year + ".xml";
                    GraphsParser.getPathToGraph(pathToXml, GraphsParser.xqPath, "/home/norexan/NetBeansProjects/weather-in-cz-gui/web/outputXQ.html", year, region, "", pathToXml);
                    Thread.sleep(1500);
                    Results.addComponent(new Label("<iframe src=\"http://localhost:8084/weather-in-cz-gui/outputXQ.html\" width = \"620\" height = \"420\" frameborder = \"0\"></iframe>", Label.CONTENT_XHTML));
                } catch (Exception ex) {
                    System.out.println(ex);
                }
                grid.removeComponent(1, 1);
                grid.addComponent(Results, 1, 1);
            }
        });

        backTosb1FromM.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                grid.removeComponent(1, 1);
                grid.addComponent(SL1, 1, 1);
            }
        });

        backTosb1FromR.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                grid.removeComponent(1, 1);
                grid.addComponent(SL1, 1, 1);
            }
        });

    }

    public String parseToHtmlString() throws Exception {
        StreamSource xml = new StreamSource(new File(year + ".xml"));
        StreamSource xslt = new StreamSource(new File("xml-to-html-month.xsl"));
        Transformer transformer = TransformerFactory.newInstance().newTransformer(xslt);
        transformer.setParameter("month", new Integer(month));

        File file = new File("/home/norexan/NetBeansProjects/weather-in-cz-gui/output.html");
        FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));

        transformer.transform(xml, new StreamResult(bw));
        fw.close();
        bw.close();

        return file.getAbsolutePath();
    }
    /*
     private void xqueryParser() throws InternalErrorException, IOException, XPathException {
     Configuration config = new Configuration();
     StaticQueryContext staticContext = config.newStaticQueryContext();
     XQueryExpression exp = staticContext.compileQuery(new FileReader("weather.xq"));

     DynamicQueryContext dynamicContext = new DynamicQueryContext(config);

     dynamicContext.setParameter("year", year);
     dynamicContext.setParameter("month", monthToInteger());
     dynamicContext.setParameter("region", region);

        

     Properties props = new Properties();
     props.setProperty(OutputKeys.METHOD, "xml");
     props.setProperty(OutputKeys.INDENT, "yes");

     File file = new File("xmls/" + year + ".xml");
     FileWriter fw = new FileWriter(file.getAbsoluteFile());
     BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
     exp.run(dynamicContext, new StreamResult(bw), props);
     fw.close();
     bw.close();

     this.setXmlFile(file);
     }*/

    public Integer monthToInteger() {
        if (month.equals("January")) {
            return 1;
        } else if (month.equals("February")) {
            return 2;
        } else if (month.equals("March")) {
            return 3;
        } else if (month.equals("April")) {
            return 4;
        } else if (month.equals("May")) {
            return 5;
        } else if (month.equals("June")) {
            return 6;
        } else if (month.equals("July")) {
            return 7;
        } else if (month.equals("August")) {
            return 8;
        } else if (month.equals("September")) {
            return 9;
        } else if (month.equals("October")) {
            return 10;
        } else if (month.equals("November")) {
            return 11;
        } else if (month.equals("December")) {
            return 12;
        } else {
            return 1;
        }
    }
}
